//
//  MessageManage.m
//  Runner
//
//  Created by 123456 on 2023/10/30.
//

#import "MessageManage.h"
#import <HelpDesk/HelpDesk.h>
#import <HyphenateChat/HyphenateChat.h>


@implementation MessageManage


+ (NSArray *)messagesToListMapWithMessages:(NSArray *)messages {
    NSMutableArray *msgs = [NSMutableArray array];
    for (id message in messages) {
        NSDictionary *messageMap = [self messageToMapWithMessage:(HDMessage *)message];
        if (messageMap) {
            [msgs addObject:messageMap];
        }
    }
    return msgs;
}

+ (NSDictionary *)messageToMapWithMessage:(HDMessage *)message {
    NSMutableDictionary *messageData = [NSMutableDictionary dictionary];
    messageData[@"direct"] = [self parseMessageDirectWithDirection:message.direction];
    messageData[@"from"] = message.from;
    messageData[@"to"] = message.to;
    messageData[@"sendTime"] = @(message.messageTime);
    messageData[@"status"] = [self parseMessageStatusWithStatus:message.status];
    messageData[@"msgId"] = message.messageId;
    messageData[@"type"] = [self parseMessageTypeWithBody:message.body];
    messageData[@"body"] = [self messageBodyWithBody:message.body];
    
    if ([message.body isKindOfClass:[EMTextMessageBody class]]) {
        messageData[@"ext"] = message.ext;
        messageData[@"extType"] = [self parseMessageExtTypeWithMessage:message];
    }
    
    return messageData;
}


+ (NSString *)parseMessageStatusWithStatus:(HDMessageStatus)status {
    switch (status) {
        case HDMessageStatusPending:
            return @"create";
        case HDMessageStatusDelivering:
            return @"inProgress";
        case HDMessageStatusSuccessed:
            return @"success";
        case HDMessageStatusFailed:
            return @"fail";
        default:
            return @"";
    }
}

+ (NSString *)parseMessageDirectWithDirection:(HDMessageDirection)direction {
    switch (direction) {
        case HDMessageDirectionSend:
            return @"send";
        case HDMessageDirectionReceive:
            return @"receive";
        default:
            return @"";
    }
}

+ (NSString *)parseMessageExtTypeWithMessage:(HDMessage *)message {
    switch ([HDMessageHelper getMessageExtType:message]) {
        case HDExtEvaluationMsg:
            return @"EvaluationMsg";
        case HDExtOrderMsg:
            return @"OrderMsg";
        case HDExtTrackMsg:
            return @"TrackMsg";
        case HDExtFormMsg:
            return @"FormMsg";
        case HDExtRobotMenuMsg:
            return @"RobotMenuMsg";
        case HDExtArticleMsg:
            return @"ArticlesMsg";
        case HDExtToCustomServiceMsg:
            return @"ToCustomServiceMsg";
        case HDExtBigExpressionMsg:
            return @"BigExpressionMsg";
        case HDExtNeedScoreMsg:
            return @"scoreMsg";
        case HDExtGeneralMsg:
            return @"GeneralMsg";
        default:
            return  @"";
    }
}


+ (NSString *)parseMessageTypeWithBody:(EMMessageBody *)body {
    if ([body isKindOfClass:[EMTextMessageBody class]]) {
        return @"text";
    } else if ([body isKindOfClass:[EMVideoMessageBody class]]) {
        return @"video";
    } else if ([body isKindOfClass:[EMImageMessageBody class]]) {
        return @"image";
    } else if ([body isKindOfClass:[EMFileMessageBody class]]) {
        return @"file";
    } else if ([body isKindOfClass:[EMCustomMessageBody class]]) {
        return @"custom";
    } else if ([body isKindOfClass:[EMVoiceMessageBody class]]) {
        return @"voice";
    } else if ([body isKindOfClass:[EMCmdMessageBody class]]) {
        return @"cmd";
    } else if ([body isKindOfClass:[EMLocationMessageBody class]]) {
        return @"location";
    }
    
    return @"";
}

+ (NSDictionary<NSString *, id> *)messageBodyWithBody:(EMMessageBody *)body {
    NSMutableDictionary<NSString *, id> *bodyMap = [NSMutableDictionary dictionary];
    
    if ([body isKindOfClass:[EMTextMessageBody class]]) {
        EMTextMessageBody *textMsg = (EMTextMessageBody *)body;
        bodyMap[@"message"] = textMsg.text;
    }
    else if ([body isKindOfClass:[EMVideoMessageBody class]]) {
        EMVideoMessageBody *videoMsg = (EMVideoMessageBody *)body;
        bodyMap[@"thumbnailUrl"] = videoMsg.thumbnailRemotePath;
        bodyMap[@"thumbnailWidth"] = @(videoMsg.thumbnailSize.width);
        bodyMap[@"thumbnailHeight"] = @(videoMsg.thumbnailSize.height);
        bodyMap[@"duration"] = @(videoMsg.duration);
        bodyMap[@"videoFileLength"] = @(videoMsg.fileLength);
        bodyMap[@"remoteUrl"] = videoMsg.remotePath;
        bodyMap[@"localUrl"] = videoMsg.localPath;
        bodyMap[@"displayName"] = videoMsg.displayName;
        bodyMap[@"fileName"] = videoMsg.displayName;
    }
    else if ([body isKindOfClass:[EMImageMessageBody class]]) {
        EMImageMessageBody *imageMsg = (EMImageMessageBody *)body;
        bodyMap[@"thumbnailUrl"] = imageMsg.thumbnailRemotePath;
        bodyMap[@"thumbnailLocalPath"] = imageMsg.thumbnailLocalPath;
        bodyMap[@"width"] = @(imageMsg.size.width);
        bodyMap[@"height"] = @(imageMsg.size.height);
        bodyMap[@"isSendOriginalImage"] = @(imageMsg.compressionRatio == 1);
        bodyMap[@"remoteUrl"] = imageMsg.remotePath;
        bodyMap[@"localUrl"] = imageMsg.localPath;
        bodyMap[@"displayName"] = imageMsg.thumbnailDisplayName;
        bodyMap[@"fileName"] = imageMsg.displayName;
        bodyMap[@"fileSize"] = @(imageMsg.fileLength);
    }
    else if ([body isKindOfClass:[EMFileMessageBody class]]) {
        EMFileMessageBody *fileMsg = (EMFileMessageBody *)body;
        bodyMap[@"remoteUrl"] = fileMsg.remotePath;
        bodyMap[@"localUrl"] = fileMsg.localPath;
        bodyMap[@"displayName"] = fileMsg.displayName;
        bodyMap[@"fileName"] = fileMsg.displayName;
    }
    else if ([body isKindOfClass:[EMCmdMessageBody class]]) {
        EMCmdMessageBody *cmdMsg = (EMCmdMessageBody *)body;
        bodyMap[@"isDeliverOnlineOnly"] = @(cmdMsg.isDeliverOnlineOnly);
        bodyMap[@"action"] = cmdMsg.action;
    }
    else if ([body isKindOfClass:[EMLocationMessageBody class]]) {
        EMLocationMessageBody *locationMsg = (EMLocationMessageBody *)body;
        bodyMap[@"address"] = locationMsg.address;
        bodyMap[@"latitude"] = @(locationMsg.latitude);
        bodyMap[@"longitude"] = @(locationMsg.longitude);
        bodyMap[@"buildingName"] = @"";
    }
    else if ([body isKindOfClass:[EMVoiceMessageBody class]]) {
        EMVoiceMessageBody *voiceMsg = (EMVoiceMessageBody *)body;
        bodyMap[@"length"] = @(voiceMsg.duration);
        bodyMap[@"remoteUrl"] = voiceMsg.remotePath;
        bodyMap[@"localUrl"] = voiceMsg.localPath;
        bodyMap[@"displayName"] = voiceMsg.displayName;
        bodyMap[@"fileName"] = voiceMsg.displayName;
        bodyMap[@"fileSize"] = @(voiceMsg.fileLength);
    }
    else if ([body isKindOfClass:[EMCustomMessageBody class]]) {
        
    }
    
    return [bodyMap copy];
}





@end
